package com.teach;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * <pre>
 * - 简单的静态http服务器
 * - 多人同时访问服务器
 * - 响应对应服务器的静态html文件
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Tel : 151 1104 7708
 * Date : 2021-5-6 10:27:05
 * Version : 1.0
 * Copyright 2021 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 */
public class HttpServer3 {
	
	private final static File root = new File("D:/_teach/2020_11_06_java/upload/作业/2021-04-05/周寒/");

	public static void main(String[] args)throws Exception {
		ServerSocket server = new ServerSocket(88);
		System.out.println("创建了http服务器:88");
		while(true) {
			Socket client = server.accept();
			new Thread(new HttpOne(client)).start();//多人同时访问服务器
			
		}
		
	}
	
	
	private static class HttpOne implements Runnable {
		
		private Socket client;

		public HttpOne(Socket client) {
			super();
			this.client = client;
		}

		@Override
		public void run() {
			try(
					InputStream in = client.getInputStream();
					BufferedReader request = new BufferedReader(new InputStreamReader(in,"UTF-8"));
					OutputStream out = client.getOutputStream();
					PrintWriter response = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
			) {
				
				String line = request.readLine();//第一行的信息
				System.out.println(line);
				String uri = line.split(" ")[1];
				System.out.println("请求的地址:"+uri);
				while(!"".equals((line=request.readLine()))) {
					System.out.println(line);
				}
				System.out.println("读取请求头信息完成");
				
				File file = new File(root,uri);
				if(file.isFile()) {
					response.println("HTTP/1.1 200 ok");
					if(uri.endsWith(".css")) {
						response.println("Content-Type:text/css;charset=UTF-8");
					}else if(uri.endsWith(".html") || uri.endsWith(".htm")) {
						response.println("Content-Type:text/html;charset=UTF-8");
					}else {
						response.println("Content-Type:aplication/stream");
					}
					
					response.println();//空行后面是响应正文
	//				response.println("<h1>你好!这是一个简单的静态http服务器</h1>");
					response.flush();
					try(
						FileInputStream fin = new FileInputStream(new File(root,uri));
					){
						byte[] bs = new byte[1024];
						int len = 0;
						while((len=fin.read(bs)) > -1) {
							out.write(bs, 0, len);
						}
						out.flush();
					}
					System.out.println("发送响应完成");
				
				}else {
					response.println("HTTP/1.1 404  没有发现文件");
					response.println("Content-Type:text/html;charset=UTF-8");
					response.println();//空行后面是响应正文
					response.println("<h1>404</h1>");
					response.println("<h2>"+uri+"没有发现</h2>");
					response.flush();
				}
				client.shutdownInput();
				client.shutdownOutput();
				
				
			}catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		
		
		
	}
	
}
